﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace NetJsWire.Widgets.Config
{
    internal class ThicknessConverter : ExpandableObjectConverter
    {
        public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
        {
            if ( destinationType == typeof( string ) )
            {
                var thick = value as Thickness;

                if ( thick.Top == 0 && thick.Right == 0 && thick.Bottom == 0 && thick.Left == 0 )
                    return "0";

                if ( thick.Top == thick.Bottom && thick.Left == thick.Right && thick.Top == thick.Left )
                    return thick.Top.ToString();

                if ( thick.Top == thick.Bottom && thick.Left == thick.Right )
                    return string.Format( "{0}, {1}", thick.Top, thick.Right ); ;

                var ret = string.Format( "{0}, {1}, {2}, {3}", thick.Top, thick.Right, thick.Bottom, thick.Left );
                return ret;
            }

            return base.ConvertTo( context, culture, value, destinationType );
        }

        public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo culture, object value )
        {
            if ( value != null && value is string )
            {
                if ( value.ToString() == "0" )
                {
                    return context.Instance;
                }
                if ( !string.IsNullOrWhiteSpace( value.ToString() ) )
                {
                    var splitted = value.ToString().Split( ',' );
                    var vals = splitted.Select( s => Convert.ToInt32( s.Trim() ) ).ToList();
                    if ( vals.Count() == 1 )
                    {
                        return new Thickness( vals[ 0 ] );
                    }

                    if ( vals.Count() == 2 )
                    {
                        return new Thickness( vals[ 0 ], vals[ 1 ] );
                    }

                    return new Thickness { Top = vals[ 0 ], Right = vals[ 1 ], Bottom = vals[ 2 ], Left = vals[ 3 ] };
                }
            }
            return base.ConvertFrom( context, culture, value );
        }

        public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
        {
            return sourceType == typeof( string ) ? true : base.CanConvertFrom( context, sourceType );
        }

        public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType )
        {
            return destinationType == typeof( Thickness ) ? true : base.CanConvertTo( context, destinationType );
        }
    }
}
